const Router = require('koa-router')
const router = new Router()
const callCloudDB = require('../utils/callCloudDB.js')
const cloudStorage = require('../utils/callCloudStorage.js')

router.get('/list', async (ctx, next) => {
    const params = ctx.request.query
    // console.log(params)
    const query = `
        db.collection('topic').skip(${params.start}).limit(${params.count}).get()
    `
    const res = await callCloudDB(ctx, 'databasequery', query)
    // console.log(res)

    //文件下载链接
    let fileList = []
    const data = res.data
    for (let i = 0, len = data.length; i < len; i++) {
        fileList.push({
            fileid: JSON.parse(data[i]).fileid,
            max_age: 7200
        })
    }
    const dlRes = await cloudStorage.download(ctx, fileList)
    // console.log(dlRes)
    let returnData = []
    for (let i = 0, len = dlRes.file_list.length; i < len; i++) {
        returnData.push({
            download_url: dlRes.file_list[i].download_url,
            _id: JSON.parse(data[i])._id,
            fileid: JSON.parse(data[i]).fileid,
            name: JSON.parse(data[i]).name,
            title: JSON.parse(data[i]).title,
        })
    }
    // console.log(returnData)

    ctx.body = {
        data: returnData,
        code: 20000,
    }
})

router.get('/getById', async (ctx, next) => {
    const query = `db.collection('topic').doc('${ctx.request.query.id}').get()`
    const res = await callCloudDB(ctx, 'databasequery', query)
    //文件下载链接
    let fileList = []
    const data = res.data

    fileList.push({
        fileid: JSON.parse(data[0]).fileid,
        max_age: 7200
    })

    const dlRes = await cloudStorage.download(ctx, fileList)
    // console.log(dlRes)
    let returnData = []

    returnData.push({
        download_url: dlRes.file_list[0].download_url,
        _id: JSON.parse(data[0])._id,
        fileid: JSON.parse(data[0]).fileid,
        name: JSON.parse(data[0]).name,
        title: JSON.parse(data[0]).title,
    })

    ctx.body = {
        code: 20000,
        data: returnData,
    }
})

router.post('/upload', async (ctx, next) => {
    // console.log(ctx.request.files.file)
    const fileid = await cloudStorage.upload(ctx, 'topic')
    // console.log(fileid)
    // 下载图片
    let fileList = []
    fileList.push({
        fileid: fileid,
        max_age: 7200
    })

    const dlRes = await cloudStorage.download(ctx, fileList)
    // console.log(dlRes)
    ctx.body = {
        code: 20000,
        id_list: dlRes
    }
})

router.post('/updatePlaylist', async (ctx, next) => {
    // console.log(ctx.request.body)
    const params = ctx.request.body
    // console.log(params)
    if (ctx.request.body.newfileid === undefined) {
        const query = `
            db.collection('topic').doc('${params._id}').update({
                data: {
                    name: '${params.name}',
                    title:'${params.title}',
                    fileid:'${params.fileid}'
                }
            })
        `
        const res = await callCloudDB(ctx, 'databaseupdate', query)
        ctx.body = {
            code: 20000,
            data: res
        }
    } else {
        // 删除云存储中的文件
        const delStorageRes = await cloudStorage.delete(ctx, params.fileid)
        const query = `
            db.collection('topic').doc('${params._id}').update({
                data: {
                    name: '${params.name}',
                    title:'${params.title}',
                    fileid:'${params.newfileid}'
                }
            })
        `
        const res = await callCloudDB(ctx, 'databaseupdate', query)
        ctx.body = {
            code: 20000,
            data: res
        }
    }
})

router.post('/add', async (ctx, next) => {
    const params = ctx.request.body
    // console.log(fileid)
    // 写数据库
    const query = `
        db.collection('topic').add({
            data: {
                name: '${params.name}',
                title:'${params.title}',
                fileid:'${params.newfileid}'
            }
        })
    `
    const res = await callCloudDB(ctx, 'databaseadd', query)
    ctx.body = {
        code: 20000,
        data: res
    }
})

router.post('/delstorage', async (ctx, next) => {
    const params = ctx.request.body
    // 删除云存储中的文件
    const delStorageRes = await cloudStorage.delete(ctx, params.newfileid)
    ctx.body = {
        code: 20000,
        data: {
            delStorageRes,
        }
    }
})

router.post('/del', async (ctx, next) => {
    const params = ctx.request.body
    // console.log(params)
    // 删除blog
    const queryBlog = `db.collection('topic').doc('${params._id}').remove()`
    const delBlogRes = await callCloudDB(ctx, 'databasedelete', queryBlog)

    // 删除图片
    const delStorageRes = await cloudStorage.delete(ctx, params.fileid)
    ctx.body = {
        code: 20000,
        data: {
            delBlogRes,
            delStorageRes
        }
    }
})

module.exports = router